package com.example.administrator.guosai7bingxiang;

import android.content.Context;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nle.mylibrary.forUse.mdbus4150.MdBus4150RelayListener;
import com.nle.mylibrary.forUse.mdbus4150.MdBus4150SensorListener;
import com.nle.mylibrary.forUse.mdbus4150.Modbus4150;
import com.nle.mylibrary.forUse.zigbee.FourChannelValConvert;
import com.nle.mylibrary.forUse.zigbee.Zigbee;
import com.nle.mylibrary.transfer.DataBusFactory;

public class MainActivity extends AppCompatActivity {
    private Button open,close,move;
    private Modbus4150 modbus4150;
    private TextView tv_temp;
    private Zigbee zigbee;
    private ImageView iv_img;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        open.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                if(motionEvent.getAction()==MotionEvent.ACTION_DOWN){
                    try {
                        modbus4150.openRelay(2, new MdBus4150RelayListener() {
                            @Override
                            public void onCtrl(boolean isSuccess) {
                                Log.i("main","open");
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }else if(motionEvent.getAction()==MotionEvent.ACTION_UP){
                    try {
                        modbus4150.closeRelay(2, new MdBus4150RelayListener() {
                            @Override
                            public void onCtrl(boolean isSuccess) {
                                Log.i("main","open");
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                return true;
            }
        });
        close.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                if(motionEvent.getAction()==MotionEvent.ACTION_DOWN){
                    try {
                        modbus4150.openRelay(3, new MdBus4150RelayListener() {
                            @Override
                            public void onCtrl(boolean isSuccess) {
                                Log.i("main","close");
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }else if(motionEvent.getAction()==MotionEvent.ACTION_UP){
                    try {
                        modbus4150.closeRelay(3, new MdBus4150RelayListener() {
                            @Override
                            public void onCtrl(boolean isSuccess) {
                                Log.i("main","close");
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                return true;
            }
        });
       Mythread mythread=new Mythread();
        mythread.start();
        move.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Animation translate=AnimationUtils.loadAnimation(MainActivity.this,R.anim.translate_animaition);
                iv_img.startAnimation(translate);


            }
        });
    }
    private void init(){
        modbus4150=new Modbus4150(DataBusFactory.newSocketDataBus("172.18.14.201",4196));
        open=findViewById(R.id.open);
        close=findViewById(R.id.close);
        zigbee=new Zigbee(DataBusFactory.newSocketDataBus("172.18.14.200",4196));
        tv_temp=findViewById(R.id.tv_temp);
        move=findViewById(R.id.move);
        iv_img=findViewById(R.id.iv_img);}
    private class  Mythread extends Thread{
        @Override
        public void run() {
            super.run();
            while (true){
                try {
                    Thread.sleep(500);
                    double[] fourEnter = zigbee.getFourEnter();
                    double temperature = FourChannelValConvert.getTemperature(fourEnter[0]);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tv_temp.setText(temperature+"°");
                        }
                    });
                    modbus4150.getVal(2, new MdBus4150SensorListener() {
                        @Override
                        public void onVal(int val) {
                            if(val==1){
                                try {
                                    modbus4150.openRelay(1, new MdBus4150RelayListener() {
                                        @Override
                                        public void onCtrl(boolean isSuccess) {
                                            Log.i("main",isSuccess+"aa");
                                        }
                                    });
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                            else if(val==0){
                                try {
                                    modbus4150.closeRelay(1, new MdBus4150RelayListener() {
                                        @Override
                                        public void onCtrl(boolean isSuccess) {
                                            Log.i("main",isSuccess+"bb");
                                        }
                                    });
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
